<?php
class StrHelper extends AppHelper{
    var $helpers = Array('Html', 'Javascript');
    var $isetc=false;
    //字符算截取
    function sub_len($strstring, $length, $etc = '...')
	{
		$string="";
		$string=$strstring;
		$string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
		$strlen = strlen($string);
		$result = '';
		for ($i = 0; (($i < $strlen) && ($length > 0)); $i++)
		{
			if ($number = strpos(str_pad(decbin(ord(substr($string, $i, 1))), 8, '0', STR_PAD_LEFT), '0'))
			{
				if ($length < 1.0)
				{
					break;
				}

				$result .= substr($string, $i, $number);
				$length -= 1.0;
				$i += $number - 1;
			}
			else
			{
				$result .= substr($string, $i, 1);
				$length -= 0.5;
			}
		}

		$result = htmlspecialchars($result, ENT_QUOTES, 'UTF-8');

		if ($i < $strlen)
		{
			$result .= $etc;
		}

		return $result;
	}
	//去除html截取
	function sub_taglen($strstring, $length)
	{
		return $this->sub_len($this->pregstring($strstring),$length);
	}
	//去除特殊字符串
	function pregstring($string){
		$string = strip_tags($string);
		$string = preg_replace ('/\n/is', '', $string);
		$string = preg_replace ('/ |　/is', '', $string);
		$string = preg_replace ('/&nbsp;/is', '', $string);
		$string = preg_replace ('/&mdash;/is', '', $string);
		$string = preg_replace ('/&ldquo;/is', '', $string);
		$string = preg_replace ('/&rdquo;/is', '', $string);
		return $string;
	}
    
    //字符算截取
    function sub_strlen($strstring, $length)
    {
        
        $string="";
        $string=$strstring;
        $string = html_entity_decode(trim(strip_tags($string)), ENT_QUOTES, 'UTF-8');
        $strlen = strlen($string);
        $result=$strstring;
        $fcount=$length-$strlen;
        //$result.=$fcount;
        for($j=0;$j<$fcount;$j++){
            $result .="&nbsp;";
        }
        return $result;
    }
    
    //提取中文首字母
    function getfirstchar($s0)
    {
        $fchar = ord($s0{0});
        if($fchar >= ord("A") and $fchar <= ord("z") )return strtoupper($s0{0});
        //	    $s1 = iconv("UTF-8","gb2312", $s0);
        //	    $s2 = iconv("gb2312","UTF-8", $s1);
        //	    if($s2 == $s0){$s = $s1;}else{$s = $s0;}
        $s = $s0;
        $asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
        if($asc>=-20319 and $asc<=-20284)return "a";
        if($asc>=-20283 and $asc<=-19776)return "b";
        if($asc>=-19775 and $asc<=-19219)return "c";
        if($asc>=-19218 and $asc<=-18711)return "d";
        if($asc>=-18710 and $asc<=-18527)return "e";
        if($asc>=-18526 and $asc<=-18240)return "f";
        if($asc>=-18239 and $asc<=-17923)return "g";
        if($asc>=-17922 and $asc<=-17418)return "h";
        if($asc>=-17417 and $asc<=-16475)return "j";
        if($asc>=-16474 and $asc<=-16213)return "k";
        if($asc>=-16212 and $asc<=-15641)return "l";
        if($asc>=-15640 and $asc<=-15166)return "m";
        if($asc>=-15165 and $asc<=-14923)return "n";
        if($asc>=-14922 and $asc<=-14915)return "o";
        if($asc>=-14914 and $asc<=-14631)return "p";
        if($asc>=-14630 and $asc<=-14150)return "q";
        if($asc>=-14149 and $asc<=-14091)return "r";
        if($asc>=-14090 and $asc<=-13319)return "s";
        if($asc>=-13318 and $asc<=-12839)return "t";
        if($asc>=-12838 and $asc<=-12557)return "w";
        if($asc>=-12556 and $asc<=-11848)return "x";
        if($asc>=-11847 and $asc<=-11056)return "y";
        if($asc>=-11055 and $asc<=-10247)return "z";
        return null;
    }
    function pykey($zh){
        $ret = "";
        $s1 = iconv("UTF-8","gb2312", $zh);
        $s2 = iconv("gb2312","UTF-8", $s1);
        if($s2 == $zh){$zh = $s1;}
        for($i = 0; $i < strlen($zh); $i++){
            $s1 = substr($zh,$i,1);
            $p = ord($s1);
            if($p > 160){
                $s2 = substr($zh,$i++,2);
                $ret .= $this->getfirstchar($s2);
            }else{
                $ret .= $s1;
            }
        }
        return $ret;
    }
    function geteducation($educationindex){
        $str_title="";
        $educations= classregistry::init('Constant')->Education;
        foreach ($educations as $key=>$education)
        {
            if($key==$educationindex){
                $str_title=$education;
                break;
            }
        }
        return $str_title;
    }
    function getsex($sex){
        $str_title=$sex=="1"?"男":"女";
        return $str_title;
    }
    function getisflag($flag){
     	return $flag=="1"?"是":"否";
    }
    function getflag($sex){
        if($sex=='1'){
            $str_title='审核通过';
        }else if($sex=='0'){
            $str_title='审核中';
        }else if($sex=='2'){
            $str_title='被驳回';
        }
        else{
        	$str_title='等待';
        }
        return $str_title;
    }
    function setprice($p){
    	$pp="无";
    	if($p!=""){
    		$pp=$p."万元";
    	}
    	return $pp;
    }
    function getafteryear($flag=1,$startyear=2008,$strz="请选择年份")
    {
        $year=date("Y",strtotime("now"));
        $objyears=array();
        if($flag){
            $objyears['']=$strz;
        }
        $yearindex=$year-$startyear;
        for ($i=0;$i<$yearindex+1;$i++){
            $objyears[$year-$i]=$year-$i;
            if(($year-$i)==$startyear){
                break;
            }
        }
        return $objyears;
    }
    function setinfo($str_title){
        if(empty($str_title)){
            $str_title="<div class='notfound'>暂无信息</div>";
        }
        return $str_title;
    }
    
    
    //获取返回文件后缀名
    function getExtension($file_name)  
    {  
        $extend = pathinfo($file_name);  
        $extend = strtolower($extend["extension"]);  
        return $extend;  
    }  
    
    function checkimg($fname)
    {
        $extension_collection = array('jpg','png','gif','bmp','jpge','pcx','tiff','dxf','cgm','cdr','wmf','eps','emf','pict');
        if(in_array($this->getExtension($fname), $extension_collection)) return true;
        else return false;
    }
    function getstausstr($index)
    {
        $str_title="";
        $Patentstatusstrs= classregistry::init('Constant')->Patentstatusstr;
        foreach ($Patentstatusstrs as $key=>$Patentstatusstr)
        {
            if($key==$index){
                $str_title=$Patentstatusstr;
                break;
            }
        }
        return $str_title;
    }
    function getstaus($index)
    {
        $str_title="";
        $Patentstatuss= classregistry::init('Constant')->Patentstatus;
        foreach ($Patentstatuss as $key=>$Patentstatus)
        {
            if($key==$index){
                $str_title=$Patentstatus;
                break;
            }
        }
        return $str_title;
    }
    function getstaustime($status,$apply_time,$accept_time,$authorize_time,$buy_time=null)
    {
        $staustime="";
        switch ($status){
            case "0":
                $staustime=$apply_time;
                break;
            case "1":
                $staustime=$accept_time;
                break;
            case "2":
                $staustime=$authorize_time;
                break;
            case "3":
                $staustime=$buy_time;
                break;
        }
        if($staustime){
            return date("Y-m-d",strtotime($staustime));
        }
        return '&nbsp;';
    }
    function getpatentto($index)
    {
        $str_title="";
        $Patenttos= classregistry::init('Constant')->Patentto;
        foreach ($Patenttos as $key=>$Patentto)
        {
            if($key==$index){
                $str_title=$Patentto;
                break;
            }
        }
        return $str_title;
    }
    function formattime($strtime)
    {
        $time="";
        if($strtime){
            return date("Y-m-d",strtotime($strtime));
        }
        return $time;
    }
    function strarea($min,$max,$index=""){
        $str_title="";
        if($min){$str_title="≥".$min;}
        if($max){$str_title="≤".$max;}
        if($index=="time"){
            if($min){$str_title="≥".$this->formattime($min);}
            if($max){$str_title="≤".$this->formattime($max);}
        }
        if($min && $max){
            $str_title=$min."～".$max;
            if($index=="time"){
                $str_title=$this->formattime($min)."～".$this->formattime($max);
            }
        }
        return $str_title;
    }
    function getkeystr($index,$objstr){
        $str_title="";
        if($index){
            foreach ($objstr as $key=>$s){
                if($index==$key){
                    $str_title=$s;
                }
            }
        }
        return $str_title;
    }
    function formatquantity($amount){
        if(empty($amount)){
            return "-";
        }else{
            return $amount=="0"?"0.00":$amount;
        }
    }
    function formatCell($string)
    {
        return $string==""?"-":$string;
    }
    function formatTextarea($remarks)
    {
        $desces = explode("\n", $remarks);
        $ds="";
        foreach ($desces as $desc)
        {
            $ds.=$desc."<br>";
        }
        return $ds;
    }
    
    function getStateValue($state_num)
    {
        $state = "";
        switch($state_num)
        {
            case "0": $state = "未";break;
            case "1": $state = "已";break;
            case "2": $state = "申请";break;
            case "3": $state = "驳回";break;
        }
        return $state;
    }
    
    function dowithtime($year,$month,$ispatent){
        $output_w_1 = "'".$year."-01-01 00:00:00'";
        $wyear = $year;
        $wmonth = $month+1;
        if($wmonth < 10){
            $wmonth = '0'.$wmonth;
        }
        if($wmonth == 13){
            $wyear = $wyear + 1;
            $wmonth = '01';
        }
        $output_w = "'".$wyear."-".$wmonth."-01 00:00:00'";
        $timewhere = "";
        if($ispatent=="patent"){
            $timewhere=" and ((authorize_time <".$output_w." and authorize_time>".$output_w_1.") or (
                             buy_time <".$output_w." and buy_time>".$output_w_1." ))";
        }else{
            $timewhere = " and authorize_time < ".$output_w."  and authorize_time>".$output_w_1;
        }
        return $timewhere;
    }
    
    function getPatents($year,$month,$cid){
        $output_w = $this->dowithtime($year,$month,"patent");
        $where = "cid = '".$cid."' and is_deleted ='0' ".$output_w." and (state = '2' or state = '3')";
       // $where = "cid = '".$cid."' and is_deleted ='0' ".$output_w." and (state = '2' or state = '3')";
        $patents = classRegistry::init('CompanyPatent')->findCount($where);
        return $patents;
    }
    
    function getProducts($year,$month,$cid,$cvid,$successDeclareId){
        $output_w = $this->dowithtime($year,$month,"pro");
        $where = "cid = '".$cid."' and type_id = '".$cvid."' and is_deleted ='0' ".$output_w." and declarate_status = '".$successDeclareId."' and state = '2' and status='1'";
       // $where = "cid = '".$cid."' and type_id = '".$cvid."' and is_deleted ='0' ".$output_w." and declarate_status = '".$successDeclareId."' and state = '2'";
        $products = classRegistry::init('CompTechnology')->findCount($where);
        return $products;
    }

    
    function getCarrier($cid){
        $carriers = classRegistry::init('CompCarrierlease')->findAll("cid = '".$cid."' and is_deleted = '0'","forbid,location");
        return $carriers;
    }
    
    function getDeclareType($cid,$successDeclareId){
        $compTalent = classRegistry::init('CompTalent');
        $compTalent->unbindModel(array ('hasMany' => array ('Attachment')));
        $types = $compTalent->findAll("cid = '".$cid."' and declarate_status = '".$successDeclareId."' and CompTalent.is_deleted = '0'","type_id");
        return $types;
    }
	//报表中百分数取整
	function setIntPercent($data){
		$int_data = intval($data);
		if(($data - $int_data) == 0){
			return $int_data;
		}else{
			return number_format($data, 2, '.', '');
		}
		
	}
	
}
?>